package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.StringOrStringArr
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.VoidFunction
import io.shuttle.mbe.core.Promise

////////////////////
// Windows
////////////////////
/**
 * Use the `Browser.windows` API to interact with browser windows. You can use this API to create, modify, and rearrange windows in the browser.
 *
 * Permissions: The Browser.windows API can be used without declaring any permission. However, the "tabs" permission is required in order to populate the url, title, and favIconUrl properties of Tab objects.
 */
interface Windows {
    // The windowId value that represents the current window.
    // default is -2
    val WINDOW_ID_CURRENT: Int

    // The windowId value that represents the absence of a Chrome browser window.
    // default is -1
    var WINDOW_ID_NONE: Int

    // Creates (opens) a new browser window with any optional sizing, position, or default URL provided.
    @PromiseStyleMinVersion(88)
    fun create(
        createData: CreateData,
        callback: Value1Function<Window?>? = null
    ): Promise<Window?>

    // Gets details about a window.
    @PromiseStyleMinVersion(88)
    fun get(
        windowId: Number,
        queryOptions: QueryOptions?,
        callback: Value1Function<Window>? = null
    ): Promise<Window>

    // Gets all windows.
    @PromiseStyleMinVersion(88)
    fun getAll(
        @PromiseStyleMinVersion(88)
        queryOptions: QueryOptions?,
        callback: Value1Function<List<Window>>? = null
    ): Promise<List<Window>>

    // Gets the current window.
    @PromiseStyleMinVersion(88)
    fun getCurrent(
        @PromiseStyleMinVersion(88)
        queryOptions: QueryOptions?,
        callback: Value1Function<Window>? = null
    ): Promise<Window>

    // Gets the window that was most recently focused — typically the window 'on top'.
    @PromiseStyleMinVersion(88)
    fun getLastFocused(
        @PromiseStyleMinVersion(88)
        queryOptions: QueryOptions?,
        callback: Value1Function<Window>? = null
    ): Promise<Window>

    // Removes (closes) a window and all the tabs inside it.
    @PromiseStyleMinVersion(88)
    fun remove(
        windowId: Number,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Updates the properties of a window. Specify only the properties that to be changed; unspecified properties are unchanged.
    @PromiseStyleMinVersion(88)
    fun update(
        windowId: Number,
        updateInfo: UpdateInfo,
        callback: Value1Function<Window>? = null
    ): Promise<Window>

    // Fired when a window has been resized; this event is only dispatched when the new bounds are committed, and not for in-progress changes.
    @PromiseStyleMinVersion(86)
    val onBoundsChanged: Events.Event<Value1Function<Window>>

    // Fired when a window is created.
    val onCreated: WindowsEvent<Value1Function<Window>?>

    // Fired when the currently focused window changes. Returns chrome.windows.WINDOW_ID_NONE if all Chrome windows have lost focus. Note: On some Linux window managers, WINDOW_ID_NONE is always sent immediately preceding a switch from one Chrome window to another.
    val onFocusChanged: WindowsEvent<Value1Function<Number>>

    // Fired when a window is removed (closed).
    @PromiseStyleMinVersion(46)
    val onRemoved: WindowsEvent<Value1Function<Number>>

    // Specifies what type of browser window to create. 'panel' is deprecated and is available only to existing allowlisted extensions on Chrome OS.
    @ChromeMinVersion(44)
    enum class CreateType {
        // Specifies the window as a standard window.
        normal,

        // Specifies the window as a popup window.
        popup,

        // Specifies the window as a panel.
        panel
    }

    @ChromeMinVersion(88)
    data class QueryOptions(
        // If true, the windows.Window object has a tabs property that contains a list of the tabs.Tab objects. The Tab objects only contain the url, pendingUrl, title, and favIconUrl properties if the extension's manifest file includes the "tabs" permission.
        var populate: Boolean?,
        // If set, the windows.Window returned is filtered based on its type. If unset, the default filter is set to ['normal', 'popup'].
        var windowTypes: List<WindowType>? = null
    )

    data class Window(
        // Whether the window is set to be always on top.
        var alwaysOnTop: Boolean,
        // Whether the window is currently the focused window.
        var focused: Boolean,
        // The height of the window, including the frame, in pixels. In some circumstances a window may not be assigned a height property; for example, when querying closed windows from the sessions API.
        var height: Number?,
        // The ID of the window. Window IDs are unique within a browser session. In some circumstances a window may not be assigned an ID property; for example, when querying windows using the sessions API, in which case a session ID may be present.
        var id: Number?,
        // Whether the window is incognito.
        var incognito: Boolean,
        // The offset of the window from the left edge of the screen in pixels. In some circumstances a window may not be assigned a left property; for example, when querying closed windows from the sessions API.
        var left: Number?,
        // The session ID used to uniquely identify a window, obtained from the sessions API.
        var sessionId: String?,
        // The state of this browser window.
        var state: WindowState?,
        // Array of tabs.Tab objects representing the current tabs in the window.
        var tabs: List<Tabs.Tab>?,
        // The offset of the window from the top edge of the screen in pixels. In some circumstances a window may not be assigned a top property; for example, when querying closed windows from the sessions API.
        var top: Number?,
        // The type of browser window this is.
        var type: WindowType?,
        // The width of the window, including the frame, in pixels. In some circumstances a window may not be assigned a width property; for example, when querying closed windows from the sessions API.
        var width: Number?,
    )

    // The state of this browser window. In some circumstances a window may not be assigned a state property; for example, when querying closed windows from the sessions API.
    @ChromeMinVersion(44)
    enum class WindowState {
        // Normal window state (not minimized, maximized, or fullscreen).
        normal,

        // Minimized window state.
        minimized,

        // Maximized window state.
        maximized,

        // Fullscreen window state.
        fullscreen,

        // Locked fullscreen window state. This fullscreen state cannot be exited by user action and is available only to allowlisted extensions on Chrome OS.
        locked_fullscreen
    }

    @ChromeMinVersion(44)
    enum class WindowType {
        // A normal browser window.
        normal,

        // A browser popup.
        popup,

        // Deprecated in this API. A Chrome App panel-style window. Extensions can only see their own panel windows.
        panel,

        // Deprecated in this API. A Chrome App window. Extensions can only see their app own windows.
        app,

        // A Developer Tools window.
        devtools
    }

    data class CreateData(
        // If true, opens an active window. If false, opens an inactive window.
        var focused: Boolean,
        // The height in pixels of the new window, including the frame. If not specified, defaults to a natural height.
        var height: Number?,
        // Whether the new window should be an incognito window.
        var incognito: Boolean?,
        // The number of pixels to position the new window from the left edge of the screen. If not specified, the new window is offset naturally from the last focused window. This value is ignored for panels.
        var left: Number?,
        // If true, the newly-created window's 'window.opener' is set to the caller and is in the same unit of related browsing contexts as the caller.
        @ChromeMinVersion(64)
        var setSelfAsOpener: Boolean?,
        // The initial state of the window. The minimized, maximized, and fullscreen states cannot be combined with left, top, width, or height.
        @ChromeMinVersion(44)
        var state: WindowState?,
        // The ID of the tab to add to the new window.
        var tabId: Number?,
        // The number of pixels to position the new window from the top edge of the screen. If not specified, the new window is offset naturally from the last focused window. This value is ignored for panels.
        var top: Number?,
        // Specifies what type of browser window to create.
        var type: CreateType?,
        // A URL or array of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme, e.g., 'http://www.google.com', not 'www.google.com'. Non-fully-qualified URLs are considered relative within the extension. Defaults to the New Tab Page.
        var url: StringOrStringArr?,
        // The width in pixels of the new window, including the frame. If not specified, defaults to a natural width.
        var width: Number?
    )

    data class UpdateInfo(
        // If true, causes the window to be displayed in a manner that draws the user's attention to the window, without changing the focused window. The effect lasts until the user changes focus to the window. This option has no effect if the window already has focus. Set to false to cancel a previous drawAttention request.
        var drawAttention: Boolean?,
        // If true, brings the window to the front; cannot be combined with the state 'minimized'. If false, brings the next window in the z-order to the front; cannot be combined with the state 'fullscreen' or 'maximized'.
        var focused: Boolean?,
        // The height to resize the window to in pixels. This value is ignored for panels.
        var height: Number?,
        // The offset from the left edge of the screen to move the window to in pixels. This value is ignored for panels.
        var left: Number?,
        // The new state of the window. The 'minimized', 'maximized', and 'fullscreen' states cannot be combined with 'left', 'top', 'width', or 'height'.
        var state: WindowState?,
        // The offset from the top edge of the screen to move the window to in pixels. This value is ignored for panels.
        var top: Number?,
        // The width to resize the window to in pixels. This value is ignored for panels.
        var width: Number?
    )

    data class WindowFilters(
        // Conditions that the window's type being created must satisfy. By default it satisfies ['normal', 'popup'].
        var windowTypes: List<WindowType>?
    )

    interface WindowsEvent<T> : Events.Event<T> {
        fun addListener(callback: T, filters: WindowFilters?)
    }
}